package com.ts.api.module.api.convert.platform.adapter;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiEncryptUtils;
import com.ts.api.module.api.convert.platform.entity.RespPlatformMultiPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMaskApply;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMaskConvertHandler;

import java.util.Map;

/**
 *  默认平台信息（卖量-对方接入）
 *
 *  @author Pan
 */
@ApiPlan(productId = "apiPlatformPhoneMaskAdapter", type = ApiPlanConvertType.PLAT_PHONE_MASK)
public class ApiPlatformPhoneMaskAdapter implements ApiPlatformPhoneMaskConvertHandler {

    private static final String KEY = "4987cd682c336e06142f2eb0722dfc05e5741c42df6171067c7c118d297743cc";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        //  屏蔽姓名
        apiDataExchangeParameter.setUserName(null);

        //  屏蔽用户id
        apiDataExchangeParameter.setUserId(null);

        //  屏蔽身份证
        apiDataExchangeParameter.setIdcard(null);

        //  屏蔽明文手机号
        if (ValidParam.isNotEmpty(apiDataExchangeParameter.getPhone())) {
            apiDataExchangeParameter.setPhone(null);
        }
        //  屏蔽手机号md5-如果存在手机号掩码模式
        if (ValidParam.isNotEmpty(apiDataExchangeParameter.getPhoneMask())) {
            apiDataExchangeParameter.setPhoneMd5(null);
        }
        return ApiEncryptUtils.encryptData(KEY, apiDataExchangeParameter);
    }

    @Override
    public RespPlatformMultiPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformMultiPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1000)) {
            return RespPlatformMultiPhoneMaskDataExchange.ofFail();
        }

        RespPlatformMultiPhoneMaskDataExchange respPlatformMultiPhoneMaskDataExchange = JsonUtils.toBean(respJson, RespPlatformMultiPhoneMaskDataExchange.class);
        respPlatformMultiPhoneMaskDataExchange.setHasBizSuc(true);
        return respPlatformMultiPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        Long productPlanId = apiApplyParameter.getProductPlanId();
        String phoneMd5 = apiApplyParameter.getPhoneMd5();

        //  非臻享屏蔽身份证
        if (!Eq.objectOr(productPlanId, 370l)) {
            apiApplyParameter.setIdcard(null);
        }

        //  用户授权协议内容页面
        apiApplyParameter.setAcceptProtocolUrl(H5DomainUrlUtils.createUserProtocolUrl(productPlanId, phoneMd5));
        //  屏蔽手机号md5
        apiApplyParameter.setPhoneMd5(null);
        return ApiEncryptUtils.encryptData(KEY, apiApplyParameter);
    }

    @Override
    public RespPlatformPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1000)) {
            return RespPlatformPhoneMaskApply.ofFail();
        }
        return RespPlatformPhoneMaskApply.ofSuc();
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        JSONObject jsonObject = JsonUtils.get(requestVO.getBody());
        String phoneMd5 = jsonObject.getString("phoneMd5");

        RespPlatformNoticeApply applyNotice = new RespPlatformNoticeApply();
        applyNotice.setPhoneMd5(phoneMd5);
        return applyNotice;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        if (apiBizStatus.hasBizSuc()) {
            return MapUtils.newHashMap(newHashMap -> {
                newHashMap.put("code", 1000);
                newHashMap.put("msg", "进件申请成功");
            });
        }
        return MapUtils.newHashMap(newHashMap -> {
            newHashMap.put("code", 0);
            newHashMap.put("msg", apiBizStatus.getBizMsg());
        });
    }
}
